const {
    createInteractionContext,
    createStateQueryClient,
    createTxSubmissionClient,
    TxSubmission,
    createChainSyncClient,
    isAlonzoBlock,
    isByronStandardBlock,
    isShelleyBlock,
    isMaryBlock,
    isAllegraBlock,
    isBabbageBlock
} = require('@cardano-ogmios/client');

const config = require('./config');


async function errorHandler(error) {
    console.error(error);
    // await client.shutdown();
}

async function closeHandler(code, reason) {
     console.log('WS close: code =', code, 'reason =', reason);
    // await client.shutdown();
    
}


const stateClient_t = async ()=>{
    let context = await createInteractionContext(errorHandler, closeHandler, {
        connection:{
            host: config.host,
            port: config.port,
            tls:true
        }
    });

    queryClient = await createStateQueryClient(context);

    let chainTip = await queryClient.chainTip()
    console.log('0 -- chainTip is : ', chainTip);

    let txInAry = new Array();
    let txInItem = {
         txId: "6b9dae80edaaa12377f13d693faa577199c9dedeae0ef05d66ee8e68b7ce2c5d",
         index: 0
    };
    let tx2={
        txId: "a34322c7d3aae2c8503ec0b1fee726651a6e9a7ddba11e04f0e76bdadcb3fe42",
        index: 0

    }
    txInAry.push(tx2);

    let utxoObjs = await queryClient.utxo(txInAry);
    console.log("1 ..... getUtxosByTxInfo...utxoObjs: ", utxoObjs);
    console.log("1.1 -- utxoObjs", utxoObjs[0][1].value);

    let genesis_config = await queryClient.genesisConfig();
    console.log("2 --- config: ", genesis_config);

    let summaries = await queryClient.eraSummaries();

    console.log("3 -- erasummaries: ", summaries);

    let protocolParameters = await  queryClient.currentProtocolParameters();

    console.log("4 -- protocolParamters: ", protocolParameters)




}

const txSubmission_t = async ()=>{
    let context = await createInteractionContext(errorHandler, closeHandler, {
        connection:{
            host: config.host,
            port: config.port,
            tls:true
        },
        interactionType:"LongRunning"

    });
    txSubmitClient = await createTxSubmissionClient(context);

    let rawTx=""

    let ret = await txSubmitClient.submitTx(rawTx);

    let ret2 = await txSubmitClient.evaluateTx(rawTx);


}
const chainSync_t = async ()=>{

    let context = await createInteractionContext(errorHandler, closeHandler, {
        connection:{
            host: config.host,
            port: config.port,
            tls:true
        },
        interactionType:"LongRunning"

    });


    let syncClient = await createChainSyncClient(context, {
        rollForward:({ block, tip }, requestNext)=>{
            console.log("in rollForwaord: block: ", block)

        },
        rollBackward:({ point ,tip}, requestNext)=>{
            console.log("in rollBackward: point: ", point)
        },
    })
    /*
    * export interface Point {
    slot: Slot;
    hash: DigestBlake2BBlockHeader;
}
    * */
    console.log("before startSync ... ");
    let points = new Array();
    points.push({
        "slot":config.initialSlot,
        "hash":config.initialHash
    })
    await syncClient.startSync(points, 1)



}

stateClient_t();

//chainSync_t();


